package model;

import context.*;

import SVG.*;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.Point2D;
import java.awt.geom.Line2D;
import java.awt.geom.Rectangle2D;
import java.awt.geom.RoundRectangle2D;
import java.awt.geom.GeneralPath;
import java.io.InputStream;
import java.io.InputStreamReader;


// ***************************************************************************
// *** CLASS: Kart
// ***************************************************************************
public class NyVeg extends Drawing2D implements Context.Constants
{ //private static final boolean DEBUG=true; // Overriding Context.Constants.DEBUG
  
//  private static InputStream bakgrunnsKart=Resources.getResourceAsStream("resources/svg/TEST.svg");
  private static InputStream bakgrunnsKart=Resources.getResourceAsStream("resources/svg/KartVedHytta.svg");
  private SVG svgKart;
  
  private static class Point
  { double x,y,z;
    Point(double x,double y,double z) { this.x=x; this.y=y; this.z=z; }
  }
  
  //*************************************************************************
  //*** CONSTRUCTOR: Kart
  //*************************************************************************
  public NyVeg()
//  { super(new Point2D.Double(-2,8),90);
  { super(new Point2D.Double(-500,-500),0.08);
    svgKart=new SVG();
    try { svgKart.read(new InputStreamReader(bakgrunnsKart,"UTF-8")); }
    catch(Exception e) { Debug.EXCEPTION("SVG.read(File) FAILED: ",e); }
    finally { try{if(bakgrunnsKart!=null) bakgrunnsKart.close();}catch(Exception ignored){} }
  }
  
  // *************************************************************************
  // *** METHOD: setPrintScaling
  // *************************************************************************
  public void setPrintScaling2(double printScale)
//  { set2DScaling(new Point2D.Double(-2,8),printScale); }
  { set2DScaling(new Point2D.Double(-300,-300),printScale); }

  // *************************************************************************
  // *** METHOD: paint
  // *************************************************************************
  public void paint(Graphics g) { paint((Graphics2D)g); }
  public void paint(Graphics2D g)
  {
//    g.translate(-500,-500);
    g.translate(origo2D.x,origo2D.y);
//    double scale=0.08;
//    g.scale(scale,scale);
    g.scale(scale2D,scale2D);
    svgKart.paint(g);
    tegnNyVeg(g);
    tegnProfil(g);
  }

  private void tegnNyVeg(Graphics2D g)
  { Color color=new Color(191,47,11);
    for(int i=0;i<veg.length;i++)
    { if((i+1)%10==0) g.setColor(Color.BLACK);
      else            g.setColor(color);
      Point p=veg[i];
      RoundRectangle2D.Double r=new RoundRectangle2D.Double(p.x,p.y,40,40,40,40);
    g.draw(r);
//    if((i+1)%10==0)
                      g.fill(r);
    }
  }

  private void tegnProfil(Graphics2D g)
  {
    float x=5300;
    float y=15000-1000;
    float w=10451;
    float h=5100+1000;
    float base=50000+5000-600-1000;
    Rectangle2D.Double r=new Rectangle2D.Double(x-10,y,w+20,h+20);
    g.setColor(Color.WHITE);
    g.fill(r);
    g.setColor(Color.BLACK);
    
    GeneralPath path=new GeneralPath();
    float kote900=base-900*40;;
    float dist=x+500+500;
    float firstX=dist;
    float z1=941;
    float z2=z1;
    float firstZ=base-z1*40;
    path.moveTo((float)dist,(float)firstZ);
    for(int i=1;i<veg.length;i++)
    { double a=veg[i].x-veg[i-1].x;
      double b=veg[i].y-veg[i-1].y;
      float delta=(float)Math.sqrt(a*a + b*b);
      if(veg[i].z > 0.5) z2=(float)veg[i].z;
      if(DEBUG) Debug.println("Profil: delta="+delta+", z1="+z1+", z2="+z2);
      float sz2=z2*40;
      float x2=dist+delta;
      float y2=base-sz2;
      path.lineTo(x2,y2);
      dist=dist+delta;
      z1=z2;
    }
    path.lineTo(dist,kote900);
    path.lineTo(firstX,kote900);
    path.closePath();
    
    g.setColor(Color.YELLOW);
    g.fill(path);
    g.setColor(Color.BLACK);
    g.draw(path);
    
    
    
    g.setFont(new Font("Arial",Font.BOLD,240));
    g.setColor(Color.BLACK);
    g.drawString("Veiprofil skisse:",x+500+500,15880+400-1000);
    
    g.setFont(new Font("Arial",Font.BOLD,140));
    float v1m=40;
    float h1m=11.5f;
    int nn=1000-4;
//    g.draw(new Line2D.Float(firstX,kote900-1000,firstX+8*nn,kote900-1000-2*nn));
//    g.drawString("Stigning: 1m pr. 4m",firstX+5000,kote900-2100);
//
//    g.draw(new Line2D.Float(firstX,kote900-1000,firstX+8*nn,kote900-1000-nn));
    g.draw(new Line2D.Float(firstX,kote900,firstX+8*80*h1m,kote900-8*10*v1m));
    g.drawString("Stigning: 1m pr. 8m",firstX+4000-1000,kote900-900);
    
    g.setFont(new Font("Arial",Font.BOLD,140));
    float h100m=1150;
    for(int i=0;i<7;i++)
    { float x1=firstX+i*h100m;
      float y1=kote900;
      float l=40;
      Line2D.Float line=new Line2D.Float(x1,y1-l,x1,y1+l);
      g.draw(line);
      switch(i)
      { case 0: g.drawString("0",x1-50,y1+300); break;
        default: g.drawString(""+(i*100)+" m",x1-150,y1+300); break;
      }
    }
    
    float v10m=10*40;
    for(int i=1;i<8;i++)
    { float x1=dist;
      float y1=kote900-(i*v10m);
      float l=40;
      Line2D.Float line=new Line2D.Float(x1-l,y1,x1+l,y1);
      g.draw(line);
      g.drawString(""+(900+i*10)+" moh",x1+100,y1+50);
    }
    
  }
  
  private static final Point[] veg=
  { new Point(9490,11590,940+1),    // 1
    new Point(9570,11630,940),  // 2
    new Point(9650,11670,940-1),    // 3
    new Point(9730,11710,940-2),    // 4
    new Point(9810,11750,940-3),    // 5
    new Point(9890,11750,940-4),    // 6
    new Point(9970,11710,940-4.5),    // 7
    new Point(10040,11670,935), // 8
    new Point(10060,11590,935-0.5),   // 9
    new Point(10060,11510,935-0.5),   // 10 - BLACK

    new Point(10065,11430,935-1.0),   // 11
    new Point(10070,11350,935-1.5),   // 12
    new Point(10075,11270,935-1.5),   // 13
    new Point(10080,11190,935-1.5),   // 14
    new Point(10095,11110,933),       // 15 ***
    new Point(10110,11030,935-1.5),   // 16
    new Point(10140,10950,935-1.5),   // 17
    new Point(10170,10870,935-1.0),   // 18
    new Point(10240,10790,935-1.0),   // 19
    new Point(10300,10710,935-0.5),   // 20 - BLACK
    
    new Point(10380,10650,935), // 21 ***
    new Point(10460,10590,935), // 22 ***
    new Point(10540,10530,935+0.25),   // 23
    new Point(10620,10480,935+0.5),   // 24
    new Point(10700,10470,935+0.75),   // 25
    new Point(10780,10470,935+1.5),   // 26
    new Point(10860,10480,935+2.0),   // 27
    new Point(10940,10500,935+2.5),   // 28
    new Point(11020,10520,935+3.0),   // 29
    new Point(11100,10540,935+3.5),   // 30 - BLACK
    
    new Point(11180,10560,935+4.0),   // 31
    new Point(11260,10580,935+4.5),   // 32
    new Point(11340,10600,940),       // 33 ***
    new Point(11420,10620,940+0.5),   // 34
    new Point(11500,10620,940+1.5),   // 35
    new Point(11580,10600,940+2.0),   // 36
    new Point(11640,10520,940+2.5),   // 37
    new Point(11660,10440,940+3.0),   // 38
    new Point(11660,10360,940+3.5),   // 39    
    new Point(11660,10280,940+4.0),   // 40 -- BLACK
    
    new Point(11640,10200,940+4.5),   // 41
    new Point(11620,10120,945),       // 42 ***
    new Point(11600,10040,945+0.5),   // 43
    new Point(11580,9960,945+1.0),    // 44
    new Point(11580,9880,945+2.5),    // 45
    new Point(11580,9800,945+3.0),    // 46
    new Point(11580,9720,945+3.5),    // 47
    new Point(11580,9640,945+4.0),    // 48
    new Point(11580,9560,945+4.5),    // 49    
    new Point(11610,9480,950),        // 50 - BLACK ***
    
    new Point(11640,9400,950+1.0),    // 51
    new Point(11670,9320,950+2.0),    // 52
    new Point(11700,9240,950+3.0),    // 53
    new Point(11760,9160,950+4.0),    // 54
    new Point(11820,9080,955),        // 55 ***
    new Point(11880,9000,955+1.0),   // 56
    new Point(11940,8920,955+2.0),    // 57
    new Point(12000-20,8840,955+3.0),   // 58
    new Point(12060-30,8760,955+4.0),  // 59    
    new Point(12120-40,8680,960),    // 60 - BLACK ***
    
    new Point(12170-60,8600,960+0.5),    // 61
    new Point(12220-80,8520,960+1.0),  // 62
    new Point(12265-100,8440,960+1.5),  // 63  
    new Point(12305-120,8360,960+2.0),  // 64  
    new Point(12340-140,8280,960+2.0),  // 65
    new Point(12400-140,8200,960+2.0),  // 66
    new Point(12450-140,8120,960+2.0),  // 67 
    new Point(12510-120,8040,960+2.0),    // 68
    new Point(12570-100,7960,960+2.5),    // 69    
    new Point(12630-80,7880,960+3.0),  // 70 - BLACK
    
    new Point(12690-60,7800,960+3.5),    // 71
    new Point(12750-40,7720,960+4.0),    // 72
    new Point(12810,7640,960+4.5),    // 73
    new Point(12860,7560,965),  // 74  ***
    new Point(12870,7480,965+1.0),    // 75
    new Point(12870,7400,965+2.0),    // 76
    new Point(12860,7320,965+3.0),    // 77
    new Point(12820,7240,965+4.0),    // 78
    new Point(12740,7225,970),  // 79
    new Point(12660,7210,970+1.0),    // 80 - BLACK
    
    new Point(12580,7195,970+2.0),    // 81
    new Point(12500,7180,970+3.0),    // 82
    new Point(12420,7165,970+4.0),  // 83    
    new Point(12340,7150,975),    // 84
    new Point(12260,7135,975+0.375),    // 85
    new Point(12180,7120,975+1*0.375),    // 86
    new Point(12100,7105,975+2*0.375),    // 87
    new Point(12020,7090,975+3*0.375),    // 88
    new Point(11940,7080,975+4*0.375),    // 89    
    new Point(11860,7075,975+5*0.375),    // 90 - BLACK
    
    new Point(11780,7070,975+6*0.375),    // 91
    new Point(11700,7070,978),  // 92
  };
  
}  
